//
//  WeatherViewController.m
//  
//
//  Created by Lawrence on 15/9/23.
//
//

#import "WeatherViewController.h"
#import <CoreLocation/CoreLocation.h>
#import "SmartLifeList.h"
#import "WeatherRequest.h"
#import <MBProgressHUD.h>


#define Header_hight  50.0f;
@interface WeatherViewController ()<MBProgressHUDDelegate>
@property(nonatomic,retain) NSMutableArray *WeatherArray;
@property(nonatomic,retain) UILabel *labCityName;
@property(nonatomic,retain) UILabel *labWeather;
@property(nonatomic,retain) UILabel *labTempMax;

@end
@implementation WeatherViewController
#pragma mark 初始化界面
-(instancetype)initWithCityCode:(NSString *)code{
    UIStoryboard *tableViewStoryboard=[UIStoryboard storyboardWithName:@"Weather" bundle:nil];
    WeatherViewController *weathView=[tableViewStoryboard instantiateViewControllerWithIdentifier:@"Weather"];
    [weathView GetWeather:code];  //网络请求获取天气
    return weathView;
}
#pragma mark 加载xib文件
-(void) registerCellNib{
    UINib *nib=[UINib nibWithNibName:@"SmartLifeListCell" bundle:nil];
    [self.tableView registerNib:nib forCellReuseIdentifier:@"SmartLifeListCell"];
}
-(instancetype)init{
    if (self=[super init]) {
        
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self registerCellNib];
    [self initTableViewHeadView];
    //[self GetWeather:code]
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)initTableViewHeadView{
    
    UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0,0, self.view.frame.size.width, self.view.frame.size.height*.3)];
    [view setAlpha:0.8f];
    [view setBackgroundColor:[UIColor colorWithRed:120/255.0 green:150/255.0 blue:220/255.0 alpha:0.8f]];
    
    self.labCityName=[[UILabel alloc] initWithFrame:CGRectMake(0,0,view.frame.size.width*.4,60)];
    self.labCityName.textColor=[UIColor whiteColor];
    self.labCityName.textAlignment=NSTextAlignmentCenter;
    self.labCityName.center=CGPointMake(self.view.center.x,view.frame.size.height*.2);
    self.labCityName.font=[UIFont systemFontOfSize:30.0f];
    
    self.labWeather=[[UILabel alloc] initWithFrame:CGRectMake(0,0,view.frame.size.width*.4,60)];
    self.labWeather.textAlignment=NSTextAlignmentCenter;
    self.labWeather.textColor=[UIColor whiteColor];
    self.labWeather.font=[UIFont systemFontOfSize:18.0f];
    self.labWeather.center=CGPointMake(self.view.center.x,view.frame.size.height*.4);
    
    
    
    [view addSubview:_labCityName];
    [view addSubview:_labWeather];
    self.tableView.tableHeaderView=view;
}
#pragma mark 规定每个section 的行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.WeatherArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    SmartLifeList *cell=[tableView dequeueReusableCellWithIdentifier:@"SmartLifeListCell" forIndexPath:indexPath];
    cell.labName.text=[_WeatherArray objectAtIndex:indexPath.row];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}
#pragma mark 更新section 头部控件文字显示天气信息
-(void)WeatherForHeaderView:(Weather *)weather{
    [self.labCityName setText:weather.city];
    [self.labWeather setText:weather.weather];
    [self.labTempMax setText:weather.temp1];
}
#pragma mark 异步机制获取天气
-(void) GetWeather:(NSString *)code{
    MBProgressHUD *HUD=[[MBProgressHUD alloc]initWithView:self.view];
    
    HUD.delegate=self;
    
    [self.view addSubview:HUD];
    
    [HUD showAnimated:YES whileExecutingBlock:^(void){
        
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0),^{
            
            Weather *cityWeather =[WeatherRequest initWithCityCode:code];
            
            dispatch_async(dispatch_get_main_queue(),^{
                
                [self WeatherForHeaderView:cityWeather];
            });
        });
        
    } completionBlock:^(void){
        
        [HUD hide:YES afterDelay:3.0f];
    }];
}
@end
